import axios, {
    type AxiosResponse,
    type AxiosRequestConfig,
    type AxiosRequestHeaders,
} from "axios";
import { useAppStore } from "@/stores/modules/test";
import { ElMessage } from "element-plus";

const request = axios.create({
    baseURL: "/api",
    timeout: 20000, // 请求过期时间
});

/* 定义response对象的data接口 */
interface ResponseData<T> {
    code: number;
    data: T;
    message: string;
}

request.interceptors.request.use((config: AxiosRequestConfig) => {
    const userStore = useAppStore();
    let token = '2d6bfb54862b4535b3ffee8c361eec72';
    if (token) {
        (config.headers as AxiosRequestHeaders).token = token;
    }
    return config;
});
// 添加响应拦截器
request.interceptors.response.use(
    (res) => {
        return res.data;
    },
    (err) => {
        return Promise.reject(err);
    }
);

export default request;
